<?php
include_once ('../../__global.php');

class apply_shop extends ml_controller
{
    protected $tao;
    public function initParam()
    {
        $this->tao = new ml_model_openapiTaobao();
        $this->o3rd=new ml_model_db3rdService();
    }
    public function checkParam()
    {


        if(empty($this->__visitor['uid'])){
            $this->redirect( SITE_ROOT_URL . '/page/login.php');
        }

        //检查是否已绑定淘宝
        $rs=$this->o3rd->get3rdServiceUser($this->__visitor['uid'],5 ,$data);
        if(!$rs){
            //todo 查询失败 处理
        }
        if(count($data)<=0){
            //未绑定淘宝 处理
            $this->redirect( SITE_ROOT_URL . '/page/shop/bind_shop.php');
        }

        $this->access_token=$data['access_token'];
    }
    public function main()
    {
        $session=self::getSession();
        $this->tao_nick=$session->getVal('3rd_nick');

        $data=$this->getUserInfo($this->tao_nick);
        $data['shop_nick']=$this->tao_nick;
        $shop=array();
        $shop=$this->getShopInfo($data['shop_url']);

        $data['is_display']=$this->display_weibo();

        $data=array_merge($data,$shop);
        $this->set_page_title2('好店申请');
        $this->page_output('shop::apply_shop' ,$data);
    }

    public function display_weibo(){

        $rs=$this->o3rd->get3rdServiceUser($this->__visitor['uid'],1 ,$data);
        if(!$rs){
            //todo 查询失败 处理
        }
        if(count($data)<=0){
           return false;
        }else{
            return true;
        }
    }


    /**
     * 获取用户信息（创建时间，好评率）
     * @param $nick
     * @return bool
     */
    public function getUserInfo($nick)
    {
        $info=$this->tao->get_user_info($nick);
        if(!$info){
            return false;
        }

        $user['good_rate']=sprintf('%01.2f',100*$info['seller_credit']['good_num']/$info['seller_credit']['total_num']);
        $time_arr=explode(' ',$info['created']);
        $user['create_time']=$time_arr[0];
        $user['shop_url']='shop'.$info['sid'].'.taobao.com';
        $this->sid=$info['sid'];
        /*
        $re=$this->tao->get_my_onsale_goods($this->access_token);
        if(!$re){
        }
        $goodsBox=$this->tao->get_data();
        */
       return $user;
    }

    public function getShopInfo($url){
        //$url='http://shop33931656.taobao.com/';

        $info=ml_tool_fetch::get_page($url);
        if(!$info){
           return false;
        }

        $preg=array(
            'goods_reg'=>'/span>(\d+)<\/li>/',
            'col_url_reg'=>'/shop(\d+)\.taobao/',
            'col_reg'=>'/<STRONG>\s*(\d+)\s*<\/STRONG>/',
        );

        $content = Tool_string::gb2utf($info['content']);

        preg_match($preg['goods_reg'],$content,$goods_match);
        $shop_info['goods_num']=empty($goods_match[1])?0 :$goods_match[1];

        $shop_info['col_url']='http://favorite.taobao.com/collect_item_relation--'.$this->sid.'--0-.htm';
        $col_info=ml_tool_fetch::get_page($shop_info['col_url']);
        $col_content = Tool_string::gb2utf($col_info['content']);

        preg_match($preg['col_reg'],$col_content,$col_match);
        $shop_info['col_num']=empty($col_match[1])?0 :$col_match[1];

        return $shop_info;
    }
}
new apply_shop();
?>